package yunquan.wang.base.process;

import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.process.workitem.wsht.SyncWSHumanTaskHandler;
import org.jbpm.task.TaskService;
import org.jbpm.task.service.local.LocalTaskService;
import org.springframework.beans.factory.InitializingBean;

public class ProcessServiceLocalJpa implements ProcessService, InitializingBean {

	private org.jbpm.task.service.TaskService jtaskService;
	private StatefulKnowledgeSession ksession;
	private TaskService taskServiceLocal;
	public void setJtaskService(org.jbpm.task.service.TaskService jtaskService) {
		this.jtaskService = jtaskService;
	}

	public void setKsession(StatefulKnowledgeSession ksession) {
		this.ksession = ksession;
	}

	@Override
	public TaskService getTaskService() {
		// 应该用这个：LocalHTWorkItemHandler
		return taskServiceLocal;

	}

	@Override
	public void afterPropertiesSet() throws Exception {

		TaskService service = new LocalTaskService(jtaskService);
		SyncWSHumanTaskHandler humanTaskHandler = new SyncWSHumanTaskHandler(
				service, ksession);
		humanTaskHandler.setLocal(true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				humanTaskHandler);
		taskServiceLocal = service;
	}
}
